package xyz.molzhao.performance;

import xyz.molzhao.util.CommonUtil;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;

public class TerribleCodeImprove {
    public static void main(String[] args) {
        CommonUtil.createBehavior("小明和小伙伴们 进餐厅点菜");
        long startTime = System.currentTimeMillis();

        ArrayList<Dish> dishes = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            dishes.add(new Dish("菜" + i, 1));
        }

        // 做菜
        ArrayList<CompletableFuture> cfList = new ArrayList<>();
        dishes.forEach(dish -> cfList.add(CompletableFuture.runAsync(dish::make)));

        // 等待所有任务执行完毕
        CompletableFuture.allOf(cfList.toArray(new CompletableFuture[cfList.size()])).join();
        CommonUtil.createBehavior("菜都做好了，上桌" + (System.currentTimeMillis() - startTime));
    }
}
